package caidan.data;

public class RecommendData
{
	private int[][][] favoriteDatas;
	// private CategoryData[] categoryDatas;
	private FoodRecommender recommender = new FoodRecommender();

	public RecommendData(CategoryData[] categoryDatas)
	{
		// this.categoryDatas = categoryDatas;
		favoriteDatas = new int[21][categoryDatas.length][];
		int chooseNum;
		for (int i = 0; i < 21; i++)
		{
			for (int j = 0; j < categoryDatas.length; j++)
			{
				chooseNum = i % 3 == 0
						? categoryDatas[j].breakfastChooseNum
						: categoryDatas[j].otherChooseNum;
				favoriteDatas[i][j] = new int[chooseNum];
				for (int k = 0; k < chooseNum; k++)
					favoriteDatas[i][j][k] = -1;
			}
		}
		for (CategoryData categoryData : categoryDatas)
			categoryData.addDataDeletedActor((Integer t, Boolean m) ->
			{
				caculateRecommendDataInThread();
			});
	}

	public int[][][] getFavoriteDatas()
	{
		return favoriteDatas;
	}

	public void caculateRecommendMealDataInThread(final int d)
	{
		new Thread(() ->
		{
			findFavoriteData(d);
		}).start();
	}

	public void caculateRecommendDataInThread()
	{
		new Thread(() ->
		{
			for (int d = 0; d < 7; d++)
				findFavoriteData(d);
		}).start();
	}

	public void caculateRecommendData()
	{
		for (int d = 0; d < 7; d++)
			findFavoriteData(d);
	}

	private void findFavoriteData(int d)
	{
		recommender.assortFood(d);
		for (int m = 0; m < 3; m++)
			setFavoriteData(d, m);
	}

	private void setFavoriteData(int d, int m)
	{
		int[][] data = favoriteDatas[d * 3 + m];
		int[][] favs = recommender.getDatas(m);
		for (int i = 0; i < data.length; i++)
			for (int j = 0; j < data[i].length; j++)
				data[i][j] = favs[i][j];
	}

	public void clearData()
	{
		for (int i = 0; i < 21; i++)
			for (int j = 0; j < favoriteDatas[i].length; j++)
				for (int k = 0; k < favoriteDatas[i][j].length; k++)
					favoriteDatas[i][j][k] = -1;
	}
}
